package org.dtrd.modules.record.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninRecordService;
import org.dtrd.modules.record.entity.bean.DailyKetoneRecordBean;
import org.dtrd.modules.record.entity.bean.KetoneStatisticBean;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.IKetoneRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;

/**
 * @Author: QR
 * @Date: 2021/10/17-0:16
 */
@Api(tags = "尿酮打卡记录")
@RestController
@RequestMapping("/101/07")
public class KetoneRecordController extends BaseController {

    @Autowired
    private IKetoneRecordService ketoneRecordService;
    @Autowired
    private IDtrdEntRdSigninRecordService signinRecordService;

    @ApiOperation("添加或修改尿酮记录")
    @PostMapping("/1010703")
    @AutoLog(value="添加或修改尿酮记录", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateKetoneRecord(@RequestBody DailyKetoneRecordBean ketoneInfo) {
        Integer patientId = ketoneInfo.getPatientId();
        // 判断 patientId 记录时间 尿酮值
        if (isIntParamInvalid(patientId)
                || StrUtil.isBlank(ketoneInfo.getRecordTime())
                || ketoneInfo.getKetoneValue() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        // 尿酮的范围在 0 到 5 之间, 其中 0阴，1+，2+弱阳，3+阳，4+强阳
        if (ketoneInfo.getKetoneValue() < 0 || ketoneInfo.getKetoneValue() > 5) {
            return error(SystemState.ResponseState.STATE_PARAMS_ERROR);
        }
        Boolean result = ketoneRecordService.saveOrUpdateKetone(ketoneInfo);
        return result ? Result.OK() : error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }

    @ApiOperation("尿酮记录查询")
    @PostMapping("/1010701")
    @AutoLog(value="尿酮记录查询", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getKetoneRecordList(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int type = getIntParam(request.getType(), 1);
        if (type == 1 || type == 3) {
            Integer page = getIntParam(request.getPage(), 1);
            Integer pageSize = getIntParam(request.getPageSize(), 20);
            String startDate = null;
            String endDate = null;
            String[] timePeriod = handleTimePeriod(request);
            if (timePeriod != null) {
                startDate = timePeriod[0];
                endDate = timePeriod[1];
            }
            if (startDate == null || endDate == null) {
                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
            }
            Integer patientId = request.getPatientId();
            Integer count = ketoneRecordService.getKetoneRecordCount(patientId, startDate, endDate);
            JSONObject result = new JSONObject();
            if (count != null && count > 0) {
                List<DailyKetoneRecordBean> list = ketoneRecordService.getAllGroupedKetoneRecord(patientId, startDate, endDate, page, pageSize);
                result.put("recordList", list);
                result.put("totalPage", count <= pageSize || pageSize <= 0 ? 1 : setTotalPage(pageSize, count));
                result.put("count", count);
            }else {
                result.put("recordList", Collections.EMPTY_LIST);
                result.put("totalPage", 1);
                result.put("count", 0);
            }
            return Result.OK(result);
        }
        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }

    /**
     * 0阴，1+，2+弱阳，3+阳，4+强阳
     */
    @ApiOperation(value = "尿酮统计查询",notes = "按时段类别统计 总次数，阴性、弱阳性、阳性、强阳性的次数")
    @PostMapping("/1010702")
    @AutoLog(value="尿酮统计查询", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getKetoneRecordStatistics(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int type = getIntParam(request.getType(), 1);
        if (type == 1 || type == 3) {
            String startDate = null;
            String endDate = null;
            String[] timePeriod = handleTimePeriod(request);
            if (timePeriod != null) {
                startDate = timePeriod[0];
                endDate = timePeriod[1];
            }
            if (startDate == null || endDate == null) {
                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
            }
            Integer patientId = request.getPatientId();
            KetoneStatisticBean ketoneRecordStatistic = ketoneRecordService.getKetoneRecordStatistic(patientId, startDate, endDate);
            return Result.OK(ketoneRecordStatistic);
        }
        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }
}
